package xp2p.adv.jxta;

import java.net.URI;
import java.net.URISyntaxException;

import net.jxta.content.ContentID;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;

public class FileAdv extends AdvBase
{
	private final static String NameTag = "name";
	private final static String FTypeTag = "ftype";
	public static final String DocType = "jxta:xp2pfileadv" ;	
	
	@Override
	protected void DefineProps()
	{
		super.DefineProps();
		Properties.put(NameTag, null);
		Properties.put(FTypeTag, null);
	}
	
	public FileAdv()
	{
		super();
	}

	@Override
	public FileAdv clone() throws CloneNotSupportedException
	{
		return new FileAdv(this.getStructuredDocument(MimeMediaType.XMLUTF8)
				.getRoot());
	}
	
	@Override
	public boolean equals(Object other)
	{
		boolean result = super.equals(other);
		if (!result && (other instanceof FileAdv) )
		{
			return this.toString().compareTo(other.toString()) == 0;
		}
		return result;		
	}
	
	@SuppressWarnings("rawtypes")
	public FileAdv(Element root)
	{
		super(root);
	}
	
	public static void RegisterSelf()
	{
		AdvertisementFactory.registerAdvertisementInstance(DocType,
				new Instantiator());
	}

	
	public static class Instantiator implements
			AdvertisementFactory.Instantiator
	{
		public String getAdvertisementType()
		{
			return FileAdv.DocType;
		}

		public Advertisement newInstance()
		{
			return new FileAdv();
		}

		@SuppressWarnings("rawtypes")
		public Advertisement newInstance(Element root)
		{
			return new FileAdv(root);
		}
	}
	
	public void setName(String name)
	{
		Properties.put(NameTag, name);
	}
	
	
	public void setFType(String name)
	{
		Properties.put(FTypeTag, name);
	}
	
	public String getName()
	{
		return (String)Properties.get(NameTag);
	}
	
	public String getFType()
	{
		return (String)Properties.get(FTypeTag);
	}

	@Override
	public String getDocType()
	{
		return DocType;
	}
	
	public ContentID getContentID()
	{
		String value = (String) Properties.get(IdTag);
		if (value == null)
		{
			return null;
		}
		ContentID id;
		try
		{
			id = ContentID.create(new URI(value));
			return id;
		} catch (URISyntaxException e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
